package com.cdut.hwadeefsc.mapper;

import com.cdut.hwadeefsc.entity.scores;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface scoresMapper {
    // 修正参数类型为Long（与实体类studentId类型一致）
    @Select("SELECT * FROM scores WHERE student_id = #{studentId}")
    List<scores> findByStudentId(Long studentId);

    // 修正参数类型为Long（与实体类studentId类型一致）
    @Select("SELECT * FROM scores WHERE student_id = #{studentId} AND semester = #{semester}")
    List<scores> findByStudentIdAndSemester(@Param("studentId") Long studentId, @Param("semester") String semester);

    // 修正字段映射（course_name→subject，补充实体类字段对应的SQL字段）
    @Insert("INSERT INTO scores (student_id, external_student_no, exam_name, subject, score, semester, exam_type, exam_date, recorded_by, created_at) " +
            "VALUES (#{studentId}, #{externalStudentNo}, #{examName}, #{subject}, #{score}, #{semester}, #{examType}, #{examDate}, #{recordedBy}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(scores score);

    // 批量插入修正字段映射
    @Insert({"<script>",
            "INSERT INTO scores (student_id, external_student_no, exam_name, subject, score, semester, exam_type, exam_date, recorded_by, created_at) VALUES ",
            "<foreach collection='list' item='item' separator=','>",
            "(#{item.studentId}, #{item.externalStudentNo}, #{item.examName}, #{item.subject}, #{item.score}, #{item.semester}, #{item.examType}, #{item.examDate}, #{item.recordedBy}, #{item.createdAt})",
            "</foreach>",
            "</script>"})
    int batchInsert(@Param("list") List<scores> scores);

    // 修正更新字段（匹配实体类属性）
    @Update("UPDATE scores SET score = #{score}, recorded_by = #{recordedBy} WHERE id = #{id}")
    int update(scores score);
}




